﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone.Scheduler;

namespace SimpleTiles.Classes
{
    public class Note : INotifyPropertyChanged
    {
        public String Titolo { get; set; }
        public String Testo { get; set; }
        public DateTime Data {get; set; }
        public DateTime Ora { get; set; }
        public String FotoURL {get; set; }
        public Boolean FontPostIt { get; set; }
        public double slideFontSize { get; set; }
        public String theme;

        [XmlIgnore]
        public Boolean Pin
        {
            get { return TileManager.ExistsTile(this); }
        }
        public Boolean Reminder
        {
            get
            {
                return ScheduledActionService.Find(this.Titolo)!=null;    
            }
        }
        public ImageSource imageSmall
        {
            get { 
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    try
                    {
                        using (IsolatedStorageFileStream isoStream = isoStore.OpenFile(this.FotoURL, FileMode.Open))
                        {
                            BitmapImage bmp = new BitmapImage();
                            bmp.SetSource(isoStream);
                            Image img = new Image();
                            img.Source = bmp;
                            return img.Source;
                        }
                    }
                    catch {
                        
                        Uri uri = new Uri("/images/ListaNote_FakeNota.png", UriKind.Relative);
                        ImageSource imgSource = new BitmapImage(uri);
                        
                        return imgSource;
                    }
                }
            }
        }
        [XmlIgnore]
        public String SrcPinImage { get { if (!Pin) return "images/pin.png"; else return "images/unpin.png"; } }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        // Serve il costruttore vuoto altrimenti il deserializzatore non riesce a creare gli oggetti!
        public Note() { }

        public Note(String titolo, String testo, DateTime data, DateTime ora, String fotoURL, Boolean FontPostIt, double slideFontSize, String theme)
        {
            this.Titolo = titolo;
            this.Testo = testo;
            this.Data = data;
            this.FotoURL = fotoURL;
            this.FontPostIt = FontPostIt;
            this.slideFontSize = slideFontSize;
            this.Ora = ora;
            this.theme = theme;
        }

        public void Update()
        {
            // Aggiungi gli altri campi se servono!
            NotifyPropertyChanged("SrcPinImage");
            NotifyPropertyChanged("imageSmall");
        }

        public override string ToString()
        {
            return "Titolo: " + Titolo + ", Testo: " + Testo + ", Data: " + Data + ", FotoURL: " + FotoURL + ", NotifyMe: " + FontPostIt;
        }
    }
}
